﻿using System.Windows;
using System.Data;
using System.Collections;
using SHomeWorkshop.LunarPresentation.Widegets;
using System.Collections.Generic;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {

        public static void GridPanelAlignment(Expressions.GridPanel.GridPanelAlignment newAlignment,
            GridPanelAlignmentTargetType targetType)
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            Expressions.Grid grid = Globals.selectedEditor.SelectedExpression as Expressions.Grid;
            if (grid == null)
            {
                //尝试看看是否GridPanel
                Expressions.GridPanel gp = Globals.selectedEditor.SelectedExpression as Expressions.GridPanel;
                if (gp == null) return;

                #region 设置一个网格子面板的对齐（或整行、整列）

                GridPanelAlignment_Panels(newAlignment, gp, targetType);

                #endregion

                return;
            }

            GridPanelAlignment_AllPanelsInGrid(newAlignment, grid);
        }

        private static void GridPanelAlignment_Panels(
            Expressions.GridPanel.GridPanelAlignment newAlignment,
            Expressions.GridPanel gridPanel, GridPanelAlignmentTargetType targetType)
        {
            if (gridPanel.Editor == null)
            {
                MessageBox.Show("发生意外，找不到此网格子面板所在的编辑器。对齐操作无法继续。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (gridPanel == null)
            {
                MessageBox.Show("发生意外，未传入有效的网格子面板实例。对齐操作无法继续。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;

            }

            if (gridPanel.ParentGrid == null)
            {
                MessageBox.Show("发生意外，找不到此网格子面板所在的父网格式。对齐操作无法继续。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            switch (targetType)
            {
                case GridPanelAlignmentTargetType.OnlySelfPanel:
                    {
                        ModifingItem mi = new ModifingItem() { OldSelectedExID = gridPanel.ExID, NewSelectedExID = gridPanel.ExID };

                        Action act = new Action(gridPanel.ExID, -1, gridPanel.GetType().Name,
                            "PanelAlign", gridPanel.PanelAlign.ToString(), newAlignment.ToString());

                        gridPanel.PanelAlign = newAlignment;

                        mi.Add(act);

                        if (mi.ModifiedPropertyAndValues.Count > 0)
                        {
                            gridPanel.Editor.Do(mi);
                            gridPanel.Editor.IsModified = true;
                        }
                        break;
                    }
                case GridPanelAlignmentTargetType.SameColumnPanels:
                    {
                        List<Expressions.SubPanel> sameColumnPanels = new List<Expressions.SubPanel>();
                        foreach (Expressions.GridPanel gp in gridPanel.ParentGrid.SubPanels)
                        {
                            if (gp.GridX == gridPanel.GridX)
                            {
                                sameColumnPanels.Add(gp);
                            }
                        }

                        if (sameColumnPanels.Count <= 0) break;

                        ModifingItem mi = new ModifingItem() { OldSelectedExID = gridPanel.ExID, NewSelectedExID = gridPanel.ExID };

                        foreach (Expressions.GridPanel gp in sameColumnPanels)
                        {
                            if (gp.PanelAlign != newAlignment)
                            {
                                Action act = new Action(gridPanel.ExID, -1, gridPanel.GetType().Name,
                                "PanelAlign", gridPanel.PanelAlign.ToString(), newAlignment.ToString());

                                gp.PanelAlign = newAlignment;

                                mi.Add(act);
                            }
                        }

                        if (mi.ModifiedPropertyAndValues.Count > 0)
                        {
                            gridPanel.Editor.Do(mi);
                            gridPanel.Editor.IsModified = true;
                        }
                        break;
                    }
                case GridPanelAlignmentTargetType.SameLinePanels:
                    {
                        List<Expressions.SubPanel> sameLinePanels = new List<Expressions.SubPanel>();
                        foreach (Expressions.GridPanel gp in gridPanel.ParentGrid.SubPanels)
                        {
                            if (gp.GridY == gridPanel.GridY)
                            {
                                sameLinePanels.Add(gp);
                            }
                        }

                        if (sameLinePanels.Count <= 0) break;

                        ModifingItem mi = new ModifingItem() { OldSelectedExID = gridPanel.ExID, NewSelectedExID = gridPanel.ExID };

                        foreach (Expressions.GridPanel gp in sameLinePanels)
                        {
                            if (gp.PanelAlign != newAlignment)
                            {
                                Action act = new Action(gridPanel.ExID, -1, gridPanel.GetType().Name,
                                "PanelAlign", gridPanel.PanelAlign.ToString(), newAlignment.ToString());

                                gp.PanelAlign = newAlignment;

                                mi.Add(act);
                            }
                        }

                        if (mi.ModifiedPropertyAndValues.Count > 0)
                        {
                            gridPanel.Editor.Do(mi);
                            gridPanel.Editor.IsModified = true;
                        }
                        break;
                    }
                case GridPanelAlignmentTargetType.SameLineAndColumnPanels:
                    {
                        List<Expressions.SubPanel> sameLinePanels = new List<Expressions.SubPanel>();
                        List<Expressions.SubPanel> sameColumnPanels = new List<Expressions.SubPanel>();

                        foreach (Expressions.GridPanel gp in gridPanel.ParentGrid.SubPanels)
                        {
                            if (gp.GridY == gridPanel.GridY)
                            {
                                sameLinePanels.Add(gp);
                            }
                        }

                        foreach (Expressions.GridPanel gp in gridPanel.ParentGrid.SubPanels)
                        {
                            if (gp.GridX == gridPanel.GridX)
                            {
                                sameColumnPanels.Add(gp);
                            }
                        }

                        ModifingItem mi = new ModifingItem() { OldSelectedExID = gridPanel.ExID, NewSelectedExID = gridPanel.ExID };

                        if (sameLinePanels.Count > 0)
                        {
                            foreach (Expressions.GridPanel gp in sameLinePanels)
                            {
                                if (gp.PanelAlign != newAlignment)
                                {
                                    Action act = new Action(gridPanel.ExID, -1, gridPanel.GetType().Name,
                                    "PanelAlign", gridPanel.PanelAlign.ToString(), newAlignment.ToString());

                                    gp.PanelAlign = newAlignment;

                                    mi.Add(act);
                                }
                            }
                        }

                        if (sameColumnPanels.Count > 0)
                        {
                            foreach (Expressions.GridPanel gp in sameColumnPanels)
                            {
                                if (gp.PanelAlign != newAlignment)
                                {
                                    Action act = new Action(gridPanel.ExID, -1, gridPanel.GetType().Name,
                                    "PanelAlign", gridPanel.PanelAlign.ToString(), newAlignment.ToString());

                                    gp.PanelAlign = newAlignment;

                                    mi.Add(act);
                                }
                            }
                        }

                        if (mi.ModifiedPropertyAndValues.Count > 0)
                        {
                            gridPanel.Editor.Do(mi);
                            gridPanel.Editor.IsModified = true;
                        }
                        break;
                    }
            }
        }

        /// <summary>
        /// 此方法不关心行、列等。只会将网格式内所有网格子面板都设置成同样的对齐方式。
        /// </summary>
        /// <param name="newAlignment"></param>
        /// <param name="grid"></param>
        private static void GridPanelAlignment_AllPanelsInGrid(Expressions.GridPanel.GridPanelAlignment newAlignment, Expressions.Grid grid)
        {
            if (grid.SubPanels.Count <= 0) return;

            ModifingItem mi = new ModifingItem() { OldSelectedExID = grid.ExID, NewSelectedExID = grid.ExID };

            foreach (Expressions.GridPanel gp in grid.SubPanels)
            {
                if (gp.PanelAlign != newAlignment)
                {
                    Action act = new Action(gp.ExID, -1, gp.GetType().Name, "PanelAlign",
                        gp.PanelAlign.ToString(), newAlignment.ToString());

                    gp.PanelAlign = newAlignment;

                    mi.Add(act);
                }
            }

            if (mi.ModifiedPropertyAndValues.Count > 0)
            {
                grid.Editor.Do(mi);
                grid.Editor.IsModified = true;
            }
        }
    }
}
